package org.dant.ant.extension.utils;

import java.io.*;
import org.apache.tools.ant.util.JavaEnvUtils;

public class StringUtils {
  public static String convert2String(String[] str) {
    if (str == null) return null;
    StringBuffer sb = new StringBuffer();
    for (int i=0; i<str.length; i++) {
      sb.append(str[i]);
      sb.append(" ");
    }
    return sb.toString();
  }

  public static String[] convert2LocalJavaCmd(String[] cmdline) {
    if (cmdline == null) return null;
    cmdline[0] = JavaEnvUtils.getJreExecutable("java");
    return cmdline;
  }

  public static String hideLongString(String str) {
    if (str != null && str.length() < 70) return str;
    StringBuffer sb = new StringBuffer();
    sb.append(str.substring(0, 35));
    sb.append("...");
    sb.append(str.substring((str.length()-20)));
    return sb.toString();
  }

  public static String duplicate(String str, int times) {
    StringBuffer sb = new StringBuffer();
    for (int i=0; i<times; i++) {
      sb.append(str);
    }
    return sb.toString();
  }
  
  public static String format(String x, int indent) {
    StringBuffer sb = new StringBuffer();
    sb.append(duplicate(" ", indent));
    sb.append(x);
    return sb.toString();
  }

  public static String formatln(String x, int indent) {
    StringBuffer sb = new StringBuffer();
    sb.append(duplicate(" ", indent));
    sb.append(x);
    sb.append("\n");
    return sb.toString();
  }

  public static String format(String x) {
    return format(x, 0);
  }

  public static String formatln(String x) {
    return formatln(x, 0);
  }

  public static void println(String x, int indent) {
    System.out.println(format(x, indent));
  }

  public static void print(String x, int indent) {
    System.out.print(format(x, indent));
  }

  public static void println(String x) {
    System.out.println(format(x, 0));
  }

  public static void print(String x) {
    System.out.print(format(x, 0));
  }

  public static File parseFileName(File basedir, String location) {
    String [] strings = location.split(":");
    return new File(basedir, strings[1]);
  }
}
